package com.bykj.taketurns.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.adapter.FragmentTabAdapter;
import com.bykj.taketurns.fragment.FindFragment;
import com.bykj.taketurns.fragment.HomeFragment;
import com.bykj.taketurns.fragment.MyFragment;
import com.bykj.taketurns.utils.Util_Rotate3DAnimation;

import java.util.ArrayList;
import java.util.List;

import kr.co.namee.permissiongen.PermissionGen;


public class HomeActivity extends BaseActivity {
    private List<Fragment> mFragmentList;
    private boolean hasFragment = false;
    private LinearLayout tabcontent;
    private LayoutInflater mLayoutInflater;
    private RadioGroup mRadioGroup;
    private CompletedAnimListener listener;
    private FragmentTabAdapter adapter;
    //加载网络数据
    @Override
    protected void postData() {

    }
    //初始化控件，在onstart中调用
    @Override
    public void initView(Bundle savedInstanceState) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    getWindow().setNavigationBarColor(Color.BLACK);
                }
        mLayoutInflater = LayoutInflater.from(this);
        tabcontent = findViewById(R.id.ll);
        mRadioGroup = findViewById(R.id.tab_line);
        mFragmentList=new ArrayList<>();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new FindFragment());
        mFragmentList.add(new MyFragment());
        hasFragment = true;


    }
    //加载fragment布局
    @Override
    public void initData(Bundle savedInstanceState) {
        adapter = new FragmentTabAdapter(mFragmentList,mRadioGroup,HomeActivity.this, R.id.realtabcontent);
    }

    @Override
    public int getParentView() {
        return R.layout.activity_home;
    }

    @Override
    public void initToolBar() {

    }

    /**
     * 设置一个新的三维旋转的容器视图。只翻一般，然后设置新的现实内容
     *
     * @param zheng    一个判断机制 如果为true 则向右翻转，如果false则向左翻转
     * @param fragment 传入的片段
     * @param start    起始位置
     * @param end      结束位置
     */
    public void applyRotation(final boolean zheng, final Fragment fragment,
                              final float start, final float end,final Fragment before) {
        // Find the center of the container
        final float centerX = tabcontent.getWidth() / 2.0f;
        final float centerY = tabcontent.getHeight() / 2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Util_Rotate3DAnimation rotation = new Util_Rotate3DAnimation(
                start, end, centerX, centerY, 310.0f, true);
        rotation.setDuration(150);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(zheng, fragment,before));// 添加监听执行现实内容的切换
        tabcontent.startAnimation(rotation);// 执行上半场翻转动画
    }
    //点击监听方法
    @Override
    public void onClick(View view) {

    }

    /**
     * 执行完上半部分旋转之后，设置要显示的新的View然后继续执行下半部分旋转
     */
    private final class DisplayNextView implements Animation.AnimationListener {
        private final boolean mPosition;
        private final Fragment mfragment;
        private final Fragment mBefore;

        private DisplayNextView(boolean zheng, Fragment fragment,Fragment before) {
            mPosition = zheng;
            mfragment = fragment;
            this.mBefore=before;
        }

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            tabcontent.post(new SwapViews(mPosition, mfragment,mBefore));// 添加新的View
            animation.cancel();
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

    /**
     * 添加要显示的新的View，并执行下半部分的旋转操作
     */
    private final class SwapViews implements Runnable {
        private final boolean mPosition;
        private final Fragment mfragment;
        private final Fragment mBefore;

        public SwapViews(boolean position, Fragment fragment,Fragment before) {
            mPosition = position;
            mfragment = fragment;
            this.mBefore=before;

        }

        public void run() {
            final float centerX = tabcontent.getWidth() / 2.0f;
            final float centerY = tabcontent.getHeight() / 2.0f;
            Util_Rotate3DAnimation rotation = null;
            FragmentTransaction tration = getSupportFragmentManager()
                    .beginTransaction();

            if (mPosition) {
                rotation = new Util_Rotate3DAnimation(-95, 0, centerX, centerY,
                        310.0f, false);
            } else {
                if(rotation!=null){
                    rotation=null;
                }
                rotation = new Util_Rotate3DAnimation(95, 0, centerX, centerY,
                        310.0f, false);
            }
            tration.show(mfragment);
            tration.hide(mBefore);
            tration.commit();
            rotation.setDuration(150);
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());
            tabcontent.startAnimation(rotation);
            rotation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if(mfragment.isAdded()){
                        listener.animEnd(animation);

                    }


                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();

        //退出时将fragment置为空，防止出现fragment重叠的现象
        if (hasFragment) {
            mFragmentList.clear();
            mFragmentList = null;
            adapter=null;

        }

    }


    //监听动画完成的接口
    public interface CompletedAnimListener{

        void animEnd(Animation animation);
    }
    public void setCompleteAnimListener(CompletedAnimListener listener){
        this.listener=listener;
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }


}
